#-*-coding:utf-8-*-
import datetime
import time
import traceback
from pprint import pprint
from scrapy import log
from pymongo.connection import MongoClient

class SingleMongodbPipeline(object):
    def __init__(self):
        try:
            client = MongoClient(self.MONGODB_SERVER,self.MONGODB_PORT)
            self.db = client[self.MONGODB_DB]
            #print 'init mongodb %s:%s/%s'%(self.MONGODB_SERVER,self.MONGODB_PORT,self.MONGODB_DB)
        except Exception as e:
            print  "ERROR(mongodb):" + str(e)
            traceback.print_exc()

    @classmethod
    def from_crawler(cls, crawler):
        cls.MONGODB_SERVER = crawler.settings.get('MONGODB_SERVER', 'localhost')
        cls.MONGODB_PORT = crawler.settings.getint('MONGODB_PORT', 27017)
        cls.MONGODB_DB = crawler.settings.get('MONGODB_DB', 'test')
        pipe = cls()
        pipe.crawler = crawler
        #print 'init mongodb setting'
        return pipe

    def process_item(self, item, spider):
        goods = {
            'url':item.get('url'),
            'name':item.get('name'),
            'attrs':item.get('attrs'),
            'desc':item.get('desc'),
            'price':item.get('price'),
            'sort':item.get('sort'),
            'brand':item.get('brand'),
            'rank':item.get('rank'),
            'intro':item.get('intro'),
            'date':datetime.datetime.now()
        }
        dbCollName = 'goods'
        source = ''
        dbColl = self.db[dbCollName]
        isSave = False
        # isOld = dbColl.find_one({"url": item.get('url')})
        # if isOld:
        #     print item.get('name') + '  -  已抓取过'.decode('utf-8')
        #     return

        if item.get('_source') != None:
            source = item.get('_source')
        if item.get('_saveDb') != None:
            dbCollName = item.get('_saveDb')
        if item.get('_isSave') != None:
            isSave = item.get('_isSave')
        if isSave:
            result = dbColl.insert(goods)
            log.msg('%s>>[SAVE][%s]:%s'%(source,item.get('sort'),item.get('name')))
        return item
